package baiwa.rta.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import baiwa.rta.dao.ComboboxDao;
import baiwa.rta.dao.mapping.RoleMapping;
import baiwa.rta.model.Role;
import baiwa.rta.model.TowerModel;

@Repository
public class ComboboxDaoImpl implements ComboboxDao {
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public List<TowerModel> findTower(TowerModel tower) {
		
		List<TowerModel> lists = new ArrayList<>();
		
		String sql = "select * from rmrmlst where 1=1 ";
		
		if(null != tower){
			if(StringUtils.isNotBlank(tower.getTower())){
				sql = sql.concat(" and RLSTTOWER  = '"+tower.getTower()+"' ");
			}
			if(StringUtils.isNotBlank(tower.getFloor())){
				sql = sql.concat(" and RLSTFLOOR  = '"+tower.getFloor()+"' ");
			}
			if(StringUtils.isNotBlank(tower.getUnitNo())){
				sql = sql.concat(" and RLSTUNITNO  = '"+tower.getUnitNo()+"' ");
			}
			if(StringUtils.isNotBlank(tower.getAddress())){
				sql = sql.concat(" and RLSTADDR  = '"+tower.getAddress()+"' ");
			}
		}
			
			lists = jdbcTemplate.query(sql, new Object[] {},
					new RowMapper<TowerModel>() {
						@Override
						public TowerModel mapRow(ResultSet rs, int index) throws SQLException {
							TowerModel model = new TowerModel();
							model.setRoomId(rs.getInt("RLRMID"));
							model.setTower(rs.getString("RLSTTOWER"));
							model.setFloor(rs.getString("RLSTFLOOR"));
							model.setUnitNo(rs.getString("RLSTUNITNO"));
							model.setAddress(rs.getString("RLSTADDR"));
							model.setType(rs.getString("RLSTTYPE"));
							return model;
						}

				});
		
		
		return lists;
	}

	@Override
	public List<Role> findActiveRole() {
		String sql = " SELECT * FROM role WHERE active = 'Y' ORDER BY ROLNME ";
		List<Role> lists= jdbcTemplate.query(sql, new Object[] {},new RoleMapping());
		return lists;
	}

}
